<template>
	<view class="body">
		<view class="navigation">
			<u-navbar title="地址选择" :autoBack="true"></u-navbar>
		</view>
		<view class="search">
			<u-search placeholder="请输入要选择的城市" :showAction="false" @change="searchAddressList()"
				v-model="search"></u-search>
		</view>
		<view class="list">
			<view v-for="(item,index) in addressList" @click="submit(item)">
				<view>{{item.detailsName}}</view>
				<view>
					<image src="@/public/right_arrow.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		rear
	} from '@/config/api.js';

	export default {
		data() {
			return {
				search: "",
				addressList: [],
				searchList: []
			}
		},
		methods: {
			searchAddressList() {
				this.addressList = []
				if (this.search != null) {
					const promise = new Promise((resolve, reject) => {
						this.getAddress(this.search, 0).then((address) => {
							resolve(address);
						});
					});
					promise.then((address) => {
						for (var i = 0; i < address.length; i++) {
							let add = address[i];
							if (add.level == "province") { //省/直辖市
								this.getAddress(add.adcode, 2).then((province) => {
									for (var j = 0; j < province[0].districts.length; j++) {
										let city = province[0].districts[j];
										for (var k = 0; k < city.districts.length; k++) {
											let district = city.districts[k];
											this.addressList.push({
												"name": district.name,
												"adcode": district.adcode,
												"detailsName": city.name + " " +
													district.name,
												"fullname": "中华人民共和国"+province[0].name + city.name +
													district.name,
												"adcodeList": [province[0].adcode, city
													.adcode, district.adcode
												]
											})
										}
									}
									this.addressList.sort((a, b) => a.adcode - b.adcode);
								});
							} else if (add.level == "city") { //地级市
								const city = new Promise((resolve2, reject2) => {
									this.getAddress(add.adcode.slice(0, 2) + "0000", 0).then((
										province) => {
										resolve2(province)
									});
								});
								city.then((province) => {
									this.getAddress(add.adcode, 1).then((
										district) => {
										for (var j = 0; j < district[0].districts.length; j++) {
											this.addressList.push({
												"name": district[0].districts[j].name,
												"adcode": district[0].districts[j].adcode,
												"detailsName":  add
													.name +" "+ district[0].districts[j].name,
												"fullname":"中华人民共和国"+ province[0].name + add
													.name + district[0].districts[j].name,
												"adcodeList": [province[0].adcode, add
													.adcode, district[0].districts[j]
													.adcode
												]
											})
										}
										this.addressList.sort((a, b) => a.adcode - b.adcode);
									});
								})
							} else if (add.level == "district") { //县/区/县级市
								const city = new Promise((resolve2, reject2) => {
									this.getAddress(add.adcode.slice(0, 2) + "0000", 0).then((
										province) => {
										resolve2(province)
									});
								});
								city.then((province) => {
									this.getAddress(add.adcode.slice(0, 4) + "00", 0).then((
										city) => {
										this.addressList.push({
											"name": add.name,
											"adcode": add.adcode,
											"detailsName": city[0]
												.name + " "+ add.name,
											"fullname": "中华人民共和国"+province[0].name + city[0]
												.name + add.name,
											"adcodeList": [province[0].adcode, city[0]
												.adcode, add.adcode
											]
										})
									});
								});
							} else { //中华人民共和国
								this.addressList = []
							}
						}
					});
				} else {
					this.addressList = []
				}

			},
			getAddress(keywords, subdistrict) {
				return new Promise((resolve, reject) => {
					uni.request({
						url: "https://restapi.amap.com/v3/config/district",
						method: 'GET',
						data: {
							keywords: keywords,
							subdistrict: subdistrict,
							key: "29fba64b6474d5732699b53c09a3129e"
						},
						success: (res) => {
							resolve(res.data.districts); // 请求成功，返回数据
						}
					});
				});
			},
			submit(item) {
				let user = uni.getStorageSync('user')
				uni.request({
					url: rear + "/user/editUser",
					method: "POST",
					data: {
						id: user.id,
						city: JSON.stringify(item)
					},
					success(res) {
						if (res.data.data) {
							user.city = item;
							uni.removeStorageSync('user');
							uni.setStorageSync('user', user);
							uni.showToast({
								title: '地址修改成功！',
								icon: 'none',
								duration: 2000
							});
							
							uni.navigateBack({
								delta: 2
							});
						} else {
							uni.showToast({
								title: '地址修改失败！',
								icon: 'none',
								duration: 2000
							});
						}
					}
				})
			},
		},

		onLoad() {

		}
	}
</script>
<style lang="scss">
	.body {
		background-color: #FDFBF9;
		height: 100vh;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;

		>* {
			margin: 3vw 5vw 0 5vw;
		}

		.search {
			margin-top: 22vw;
		}

		.list {
			overflow: hidden;
			flex-grow: 1;
			overflow-y: auto;

			>view {
				display: flex;
				flex-wrap: wrap;
				line-height: 10vw;

				>view:nth-of-type(1) {
					width: 90%;
				}

				>view:nth-of-type(2) {
					width: 10%;
					text-align: right;

					image {
						width: 2vw;
					}
				}
			}
		}
	}
</style>